package com.hf.hantopy.controller;

import com.hf.hantopy.mapper.CarAdminMapper;
import com.hf.hantopy.model.CarAdmin;
import com.hf.hantopy.utils.excel.CommonUtil;
import com.hf.hantopy.utils.excel.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Date: 2019/2/26
 * @Auther: wm yu
 */
@RestController
@RequestMapping("/excel")
public class MyController {
  @Autowired
  private CarAdminMapper carAdminMapper;

    @PostMapping("/export")
    public void export(HttpServletResponse response){
        String title = "车管信息列表";
        List<String> headList = new ArrayList<>();
        List<CarAdmin> list = carAdminMapper.findAll();
        //重组数据

        if(!CollectionUtils.isEmpty(list)){
            CommonUtil.getAnnoFields(list.get(0).getClass(),headList);
            ExcelUtil.downloadExcelFile(title,headList,list,response);
        }
    }

    @PostMapping("/importExcel")
    public List<Object> importExcel(MultipartFile file, HttpServletRequest request){
        try {
            List<Map<String,Object>> list = ExcelUtil.getData(file);
            List<Object> objects = CommonUtil.ExcelData2Object(list, CarAdmin.class);
            return objects;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
